﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    public partial class SupplierPicker : UserControl, IView
    {
        #region fileds
        private int _pageIndex;
        private int _pageCount;
        #endregion

        public SupplierPicker()
        {
            InitializeComponent();

            ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region override
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadSupplierListData();
        }
        #endregion

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", "确定", OnOKActionClick))
                .AddAction(new ActionNode("cancel", "取消", OnCancelActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region actions
        private void OnOKActionClick(object sender, object e)
        {
            int[] selRows = ctlGridView.GetSelectedRows();
            if(selRows==null || selRows.Length == 0)
            {
                return;
            }
            WebRef.VSupplier supplier = ctlGridView.GetRow(selRows[0]) as WebRef.VSupplier;
            UIContext.Current.ViewManager.CloseView(true, supplier);
        }

        private void OnCancelActionClick(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        #endregion

        #region methods
        private void LoadSupplierListData()
        {
            this.ShowMask();
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VSupplier> result = new Service.QueryService().QuerySupplierPagedList(GetFilter(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.VSupplier>(result,
                            out _pageIndex,
                            out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                    }
                    this.HideMask();
                }));
            });
        }

        private int GetPageLimit()
        {
            return 25;
        }

        public string GetFilter()
        {
            return ctlEditFilter.Text.Trim();
        }
        #endregion

        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            LoadSupplierListData();
        }
    }
}
